﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Engine.Init {
    import flash.events.*;
    import Engine.Managers.*;
    import flash.utils.*;
    import Engine.*;

    public class InitializationManager extends EventDispatcher {

        private static const REPORT_DELAY:int = 10000;

        protected static var m_instance:InitializationManager;
        protected static var m_doActionsLock:Boolean;
        protected static var m_statsReporting:Boolean = false;

        protected var m_actions:Array;
        protected var m_runningActions:Array;
        protected var m_completedActions:Array;
        protected var m_loadTimer:Timer;
        protected var m_CompleteTimeRecorded:Boolean = false;
        protected var m_CompleteEventFired:Boolean = false;
        protected var m_startTime:Number;

        public function InitializationManager(){
            super(null);
            if (InitializationManager.m_instance == null){
                InitializationManager.m_instance = this;
            };
            this.m_actions = new Array();
            this.m_runningActions = new Array();
            this.m_completedActions = new Array();
            m_doActionsLock = false;
            if (Math.floor(Utilities.randBetween(1, 100)) == 50){
                m_statsReporting = true;
            };
            this.m_startTime = GlobalEngine.getTimer();
            if (m_statsReporting){
                StatsManager.count("Initialization", "Begin");
                this.m_loadTimer = new Timer(REPORT_DELAY);
                this.m_loadTimer.addEventListener(TimerEvent.TIMER, this.onTimerCallback);
                this.m_loadTimer.start();
            };
        }
        public static function getInstance():InitializationManager{
            return (m_instance);
        }

        public function add(_arg1:InitializationAction):void{
            this.m_actions.push(_arg1);
        }
        public function execute():void{
            this._doActions();
        }
        public function hasActionCompleted(_arg1:String):Boolean{
            return ((this.m_completedActions.indexOf(_arg1) >= 0));
        }
        public function getDependentActions(_arg1:String):Array{
            var _local4:InitializationAction;
            var _local2:Array = [];
            var _local3:int;
            while (_local3 < this.m_actions.length) {
                _local4 = this.m_actions[_local3];
                if (_local4.getDependencies().indexOf(_arg1) >= 0){
                    _local2.push(_local4);
                };
                _local3++;
            };
            return (_local2);
        }
        private function _getIncompleteActions():Array{
            var _local3:InitializationAction;
            var _local1:Array = new Array();
            var _local2:int;
            while (_local2 < this.m_actions.length) {
                _local3 = this.m_actions[_local2];
                if (!_local3.hasCompleted()){
                    _local1.push(_local3);
                };
                _local2++;
            };
            return (_local1);
        }
        private function _isActionRunning(_arg1:String):Boolean{
            return ((this.m_runningActions.indexOf(_arg1) >= 0));
        }
        private function _doActions(_arg1:Event=null):void{
            var _local4:Timer;
            var _local5:InitializationAction;
            if (m_doActionsLock){
                _local4 = new Timer(100, 1);
                _local4.addEventListener(TimerEvent.TIMER_COMPLETE, this._doActions);
                _local4.start();
                return;
            };
            m_doActionsLock = true;
            var _local2:Array = this._getIncompleteActions();
            if ((((this.m_runningActions.length <= 0)) && ((_local2.length <= 0)))){
                if (!this.m_CompleteEventFired){
                    this.m_CompleteEventFired = true;
                    dispatchEvent(new Event(Event.COMPLETE));
                    this.recordFinishTime();
                };
            };
            var _local3:int;
            while (_local3 < _local2.length) {
                _local5 = _local2[_local3];
                if (((!(this._isActionRunning(_local5.getActionId()))) && (_local5.isReady()))){
                    GlobalEngine.msg("Init", (_local5.getActionId() + " started"));
                    _local5.startTime = getTimer();
                    _local5.addEventListener(Event.COMPLETE, this._onActionComplete);
                    this.m_runningActions.push(_local5.getActionId());
                    _local5.execute();
                };
                _local3++;
            };
            m_doActionsLock = false;
        }
        public function haveAllCompleted():Boolean{
            return ((((this.m_runningActions.length <= 0)) && ((this._getIncompleteActions().length <= 0))));
        }
        private function _onActionComplete(_arg1:Event):void{
            var _local8:InitializationAction;
            var _local9:String;
            var _local2:InitializationAction = (_arg1.target as InitializationAction);
            var _local3:Number = ((getTimer() - _local2.startTime) / 1000);
            GlobalEngine.log("Init", (((_local2.getActionId() + " completed (") + _local3) + " sec)"));
            this.m_completedActions.push(_local2.getActionId());
            var _local4:ProgressEvent = new ProgressEvent(ProgressEvent.PROGRESS);
            _local4.bytesLoaded = 1;
            _local4.bytesTotal = 1;
            var _local5:int;
            while (_local5 < this.m_actions.length) {
                _local8 = this.m_actions[_local5];
                _local4.bytesLoaded = (_local4.bytesLoaded + _local8.progress);
                _local4.bytesTotal = (_local4.bytesTotal + _local8.weight);
                _local5++;
            };
            dispatchEvent(_local4);
            var _local6:Array = new Array();
            var _local7:int;
            while (_local7 < this.m_runningActions.length) {
                _local9 = this.m_runningActions[_local7];
                if (_local9 != _local2.getActionId()){
                    _local6.push(_local9);
                };
                _local7++;
            };
            this.m_runningActions = _local6;
            this._doActions();
        }
        private function _actionById(_arg1:String):InitializationAction{
            var _local4:InitializationAction;
            var _local2:InitializationAction;
            var _local3:int;
            while ((((_local2 == null)) && ((_local3 < this.m_actions.length)))) {
                _local4 = (this.m_actions[_local3] as InitializationAction);
                if (_local4.getActionId() == _arg1){
                    _local2 = _local4;
                };
                _local3++;
            };
            return (_local2);
        }
        private function onTimerCallback(_arg1:TimerEvent):void{
            if (this.haveAllCompleted()){
                StatsManager.count("Initialization", "Finished", (((this.m_loadTimer.currentCount * REPORT_DELAY) / 1000) + " seconds"));
                this.recordFinishTime();
                this.m_loadTimer.stop();
                this.m_loadTimer = null;
            } else {
                StatsManager.count("Initialization", "Not Finished", (((this.m_loadTimer.currentCount * REPORT_DELAY) / 1000) + " seconds"), ("Actions running: " + this.m_runningActions.toString()));
            };
        }
        private function recordFinishTime():void{
            var _local1:Number;
            if (!this.m_CompleteTimeRecorded){
                this.m_CompleteTimeRecorded = true;
                if (m_statsReporting){
                    _local1 = (GlobalEngine.getTimer() - this.m_startTime);
                    StatsManager.count("Initialization", "Elapsed Time", "", "", "", "", Math.floor((_local1 / 1000)));
                };
            };
        }
        public function getRunningActionsString():String{
            var _local1 = "initNoRunningActions";
            if (this.m_runningActions.length > 0){
                _local1 = ("init:" + this.m_runningActions.join(","));
            };
            return (_local1);
        }

    }
}//package Engine.Init 
